package com.shujia.core

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Demo01WordCount {
  def main(args: Array[String]): Unit = {

    val conf: SparkConf = new SparkConf()
    conf.setAppName("Demo01WordCount")
    conf.setMaster("local") // Spark 运行的方式

    // 构建Spark环境：SparkContext
    val sc: SparkContext = new SparkContext(conf)

    // 读取数据
    val rdd: RDD[String] = sc.textFile("spark/data/words.txt")

    rdd
      .flatMap(_.split(","))
      .groupBy(word => word)
      .map(kv => {
        val word: String = kv._1
        val cnt: Int = kv._2.size
        s"$word, $cnt"
      }).foreach(println)

    while (true) {

    }


  }

}
